package com.acmetelecom.core;

import static com.acmetelecom.call.CallEventBuilder.aCallFrom;
import static com.acmetelecom.call.CallEventBuilder.asDateTime;
import static com.acmetelecom.call.CallEventBuilder.first;
import static com.acmetelecom.customer.CustomerBuilder.aCustomerNamed;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.math.BigDecimal;
import java.util.Arrays;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.acmetelecom.call.CallEnd;
import com.acmetelecom.call.CallLogger;
import com.acmetelecom.call.CallStart;
import com.acmetelecom.customer.CentralCustomerDatabase;
import com.acmetelecom.customer.CentralTariffDatabase;
import com.acmetelecom.customer.Customer;
import com.acmetelecom.customer.Tariff;

public class BillingSystemTest {
	
	private BillingSystem billSys;
	
	@Mock
	private BillGenerator billGenerator;

	private Customer tim, bob;
	
	@Mock
	private CentralTariffDatabase tariffDb;

	private Tariff tariff = Tariff.Business;

	@Mock
	private CentralCustomerDatabase customerDb;

	@Mock
	private CallLogger callLogger;
	
	@Before
	public void before() {
		MockitoAnnotations.initMocks(this);
		tim = aCustomerNamed("Tim").withPhoneNo("+4413345").withThePricePlan("Business");
		bob = aCustomerNamed("Bob").withPhoneNo("+4413645").withThePricePlan("Business");
		when(tariffDb.tarriffFor(tim)).thenReturn(tariff);
		when(customerDb.getCustomers()).thenReturn(Arrays.asList(tim));
		billSys = new BillingSystem(callLogger, billGenerator, tariffDb, customerDb);
	}
	
	@Test
	public void testSimpleCall() {
		CallStart e1 = aCallFrom(tim).to(bob).thatStartedAt( asDateTime("02/12/11 4:00pm"));
		CallEnd e2 = aCallFrom(tim).to(bob).endedAt( asDateTime("02/12/11 4:05pm"));
		when(callLogger.getCompletedCalls(tim)).thenReturn(Arrays.asList(first(e1).then(e2)));
		billSys.createCustomerBills();
		verify(billGenerator).send(tim, Arrays.asList(new LineItem(first(e1).then(e2), BigDecimal.valueOf(90))));
	}
}
